var yljgdm;
var startTime;
var endTime;
var name;
var idCard;

function getUrlParam(param) {
    var params = Ext.urlDecode(location.search.substring(1));
    return param ? params[param] : params;
}

var param_yljgdm = getUrlParam('yljgdm');
var param_checkTime = getUrlParam('checkTime');

function init() {
    if (Ext.LoadMask) { //自定义loading的提示语 add by zhangwenkai
        Ext.LoadMask.prototype.msg = "读取中...";
    }

    var yljgdmStore = Ext.create('Ext.data.TreeStore', {
        fields: [{
            name: 'id',
            type: 'id'
        }, {
            name: 'text',
            type: 'text'
        }],
        proxy: {
            type: 'ajax',
            // url: 'http://localhost:8080/HealthPlatform/userMgr/getUserOrganization?_dc=1602318175693&node=root'
            url: 'http://127.0.0.1:8020/demo/Ext.js/Ext.4.2-%E6%A8%A1%E6%8B%9F%E7%89%88%E6%9C%AC/tree.json'
        },
        autoLoad: true
    });
    console.log(yljgdmStore)

    var tbar1 = [{
            xtype: 'comboboxtree',
            id: 'yljgdm',
            name: 'yljgdm',
            fieldLabel: '医疗机构<font color=red>*</font>',
            valueField: 'id',
            editable: false,
            allowBlank: false,
            displayField: 'text',
            multiSelect: false, //是否可以多选
            multiCascade: false, //是否级联选择
            rootVisible: false, //是否显示根节点
            selectNodeModel: 'all',
            minPickerHeight: 250, //默认最小高度
            width: 450,
            store: yljgdmStore,
            trigger1Cls: 'x-form-clear-trigger',
            trigger2Cls: 'x-form-arrow-trigger',
            onTrigger1Click: function () {
                this.clearValue();
            },
            onTrigger2Click: function () {
                this.onTriggerClick()
            },
        },
        '-',
        {
            name: 'start_time',
            id: 'start_time_id',
            mode: 'local',
            fieldLabel: '开始时间',
            xtype: 'datefield',
            format: 'Y-m-d',
            width: 250,
            value: param_checkTime != undefined ? new Date(param_checkTime) : new Date(new Date() - 24 * 60 * 60 * 1000),
            listeners: {
                'select': function () {
                    var start = Ext.getCmp('start_time_id').getValue();
                    Ext.getCmp('end_time_id').setMinValue(start);
                    var endDate = Ext.getCmp('end_time_id').getValue();
                    if (start > endDate) {
                        Ext.getCmp('end_time_id').setValue(start);
                    }
                }
            }
        }, '-', {
            name: 'end_time',
            id: 'end_time_id',
            mode: 'local',
            fieldLabel: '截止时间',
            xtype: 'datefield',
            format: 'Y-m-d',
            width: 250,
            value: param_checkTime != undefined ? new Date(param_checkTime) : new Date(new Date() - 24 * 60 * 60 * 1000),
            listeners: {
                select: function () {
                    var start = Ext.getCmp('start_time_id').getValue();
                    var endDate = Ext.getCmp('end_time_id').getValue();
                    if (start > endDate) {
                        Ext.getCmp('start_time_id').setValue(endDate);
                    }
                }
            }
        }
    ];


    var tbar2 = [{
            name: 'name',
            xtype: 'textfield',
            id: 'name',
            width: 268,
            fieldLabel: '姓名'
        },
        '-', {
            name: 'idCard',
            xtype: 'textfield',
            id: 'idCard',
            width: 250,
            fieldLabel: '身份证号'
        },
        '-', {
            text: '检索',
            tooltip: '检索',
            iconCls: 'icon-search',
            handler: function () {
                search();
            }
        }
    ];

    //查询数据
    function search() {
        store.load();
    }


    // 数据
    var store = Ext.create('Ext.data.Store', {
        fields: ['GUID', 'ORG_NAME', 'XM', 'XB', 'ZJHM', 'CSRQ', 'JZKSMC', 'JZKSRQ', 'ZZYSXM', 'MZZDMC'],
        //设置分页大小
        pageSize: 20,
        proxy: {
            type: 'ajax',
            url: '',
            // url: '/HealthPlatform/healthQuery/getMzInforQuery',
            reader: {
                type: 'json',
                root: 'data.data',
                //获取数据总数
                totalProperty: 'data.total'
            }
        },
        listeners: {
            'beforeload': function (store) {
                Ext.apply(store.proxy.extraParams, {
                    yljgdm: Ext.getCmp('yljgdm').getValue(),
                    startTime: Ext.getCmp('start_time_id').getValue(),
                    endTime: Ext.getCmp('end_time_id').getValue(),
                    name: Ext.getCmp("name").getValue(),
                    idCard: Ext.getCmp("idCard").getValue()
                });
            }
        },
        autoLoad: true
    });


    // grid
    var ggidpanel = Ext.create('Ext.grid.GridPanel', {
        id: 'exportgrid',
        store: store,
        columns: [
            new Ext.grid.RowNumberer({
                header: '序号',
                align: 'center',
                renderer: function (value, metadata, record, rowIndex) {
                    return (record.store.currentPage - 1) * record.store.pageSize + 1 + rowIndex;
                },
                width: 36
            }),
            {
                dataIndex: 'GUID',
                header: 'PK',
                hidden: true
            },
            {
                text: '医疗机构名称',
                dataIndex: 'ORG_NAME',
                type: 'string',
                align: 'left',
                flex: 1.2
            },
            {
                text: '姓名',
                dataIndex: 'XM',
                align: 'center',
                flex: 0.5
            },
            {
                text: '性别',
                dataIndex: 'XB',
                align: 'center',
                flex: 0.5
            },
            {
                text: '证件号码',
                align: 'center',
                dataIndex: 'ZJHM',
                flex: 1.5
            },
            {
                text: '出生日期',
                dataIndex: 'CSRQ',
                align: 'center',
                flex: 1
            },
            {
                text: '就诊科室',
                dataIndex: 'JZKSMC',
                align: 'center',
                flex: 1
            },

            {
                text: '就诊日期',
                dataIndex: 'JZKSRQ',
                align: 'center',
                flex: 1
            },

            {
                text: '主诊医生',
                dataIndex: 'ZZYSXM',
                align: 'center',
                flex: 1
            },
            {
                text: '门诊诊断名称',
                dataIndex: 'MZZDMC',
                align: 'center',
                flex: 1
            },
            {
                text: '档案调阅',
                align: 'center',
                flex: 1,
                renderer: function (value, metaData, record, rowIndex, colIndex, store, view) {
                    var idCard = record.data.GUID;
                    if (!idCard) {
                        return "无个档";
                    }
                    return "<button onclick=newWin('" + idCard + "')  onmouseover=\"this.style.cursor='hand'\">调阅</button>";
                }
            }
        ],
        listeners: {
            render: function (p) {
                p.setLoading({
                    store: p.getStore()
                }).hide();
            }
        },
        tbar: {
            xtype: "container",
            border: false,
            items: [{
                xtype: "toolbar",
                items: tbar1
            }, {
                xtype: "toolbar",
                items: tbar2
            }]
        },
        bbar: new Ext.PagingToolbar({
            displayInfo: true,
            store: store
        })
    });

    // 初始页面
    new Ext.Viewport({
        layout: 'fit',
        defaults: {
            border: false
        },
        items: [{
            layout: 'border',
            defaults: {
                border: false
            },
            items: [{
                region: 'center',
                layout: 'fit',
                autoScroll: true,
                defaults: {
                    border: false
                },
                items: [ggidpanel]
            }]
        }]
    });
}

function newWin(idCard) {
    window.open('/HealthPlatform/newHealthRecords/index?idCard=' + idCard);
}

Ext.onReady(init);